package com.boot.mall.order.web;

import com.boot.mall.order.entity.OrderEntity;
import com.boot.mall.order.service.OrderService;
import com.boot.mall.order.vo.OrderConfirmVo;
import com.boot.mall.order.vo.OrderListVo;
import com.boot.mall.order.vo.OrderSubmitVo;
import com.boot.mall.order.vo.SubmitOrderResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.List;
import java.util.concurrent.ExecutionException;

/**
 * 订单
 */
@Controller
public class OrderWebController {

    @Autowired
    private OrderService orderService;

    /**
     * 去订单详情页面
     * @param model
     * @return
     * @throws ExecutionException
     * @throws InterruptedException
     */
    @GetMapping("/toTrade")
    public String toTrade(Model model) throws ExecutionException, InterruptedException {
        OrderConfirmVo confirmVo = orderService.getConfirmOrder();
        model.addAttribute("confirmOrder",confirmVo);
        return "confirm";
    }

    /**
     * 下单操作
     * @param vo
     * @return
     */
    @PostMapping("/submitOrder")
    public String submitOrder(OrderSubmitVo vo,Model model) throws ExecutionException, InterruptedException {
        // 创建订单
        SubmitOrderResponseVo responseVo = orderService.createSubmitOrder(vo);
        if (responseVo.getCode() == 0){
            model.addAttribute("order",responseVo.getOrder());
            return "pay";
        }else {
            return "redirect:http://order.guli.com/toTrade";
        }
    }

    /**
     * 支付成功之后跳转订单页面
     * @return
     */
    @GetMapping("/memberOrder.html")
    public String memberOrderList(Model model){
        //http://order.gulimall.com/memberOrder.html?charset=utf-8&out_trade_no=202106062013288681401512564474351618&method=alipay.trade.page.pay.return&total_amount=7599.00&sign=fLplHMp%2BwhDXMC9%2Bcg9PPG4%2F%2B53VAnbcmPTWsEF8pR%2FSosYKA1tZ4p2QAPpJBJLhvzAMoepCgPy8ISr0x7Aw2FAL%2BZttg7kjI0ElVxbicNvh1sm6giYCSbOdsEGlrKbFGWLBUHbRLKvhDxD%2BKLnYTbcVgg4VNx70gXPly3qEztusJK0O35LD1ERVIWZi2KhJ3Otdeyu8yL0RYfn3OXEfmlvENIqA9qwCPydyisNh0cxRaJBazVjmKJaseeuG2tGMAIs4IM8IggLQ99%2Bnq5yHF5kxXDjtGRkKdWCH%2B9QbDYaWWWsWs8WzgjKBKlPUJtzg9uGXROvre4bkV7ZTVn4QxQ%3D%3D&trade_no=2021060622001498480501711944&auth_app_id=2021000117671198&version=1.0&app_id=2021000117671198&sign_type=RSA2&seller_id=2088621955928269&timestamp=2021-06-06+20%3A13%3A55
        List<OrderListVo> entities = orderService.listMemberOrderList();
        model.addAttribute("orderLists",entities);
        return "list";
    }

}
